import 'package:homepad/iot/basic_node.dart';
import 'package:homepad/iot/basic_types.dart';
import 'package:homepad/iot/message_def.dart';

class Fan extends BasicNode {
  Fan.fromJson(super.json): super.fromJson();

  int fanSpeed = 20;
  void setFanSpeed(int speed) {
    fanSpeed = speed;
    delegate.onStateChanged("ep:$endpointId-speed:$speed");

    delegate.onAttributeSet(
        AttributePathIB(
            endpoint: endpointId,
            cluster: ClusterIds.fanControl,
            attribute: FanAttributeId.fanSetting
        ), speed);
  }

  @override
  void readAllState() {
    readAttribute(ClusterIds.fanControl, FanAttributeId.fanSetting);
  }

  @override
  bool onAttributeReport(AttributePathIB path, dynamic data) {
    if (path.cluster != ClusterIds.fanControl) {
      return super.onAttributeReport(path, data);
    }

    if (path.attribute == FanAttributeId.fanSetting) {
      final newValue = data as int;
      if (newValue == fanSpeed) {
        return false;
      }
      fanSpeed = newValue;
      return true;
    }
    return false;
  }
}